from functools import wraps
from flask import g
from app.user.model import User
from common.mixins import DictMixin
from exts import db
from app.user.model import Role


class PermissionKey:
    USER_GET = "USER_GET"
    USER_GET_ALL = "USER_GET_ALL"
    USER_CREATE = "USER_CREATE"
    USER_EDIT = "USER_EDIT"
    USER_DELETE = "USER_DELETE"

    ROLE_GET = "ROLE_GET"
    ROLE_CREATE = "ROLE_CREATE"
    ROLE_EDIT = "ROLE_EDIT"
    ROLE_DELETE = "ROLE_DELETE"



    PRODUCT_GET = 'PRODUCT_GET'
    PRODUCT_EDIT = 'PRODUCT_EDIT'
    PRODUCT_DELETE = 'PRODUCT_DELETE'
    STAFF = 'STAFF'
    SALE = "SALE"
    PURCHASE = "PURCHASE"
    PURCHASE_GET_PURCHASE_PRICE = "PURCHASE_GET_PURCHASE_PRICE"

    OPERATION_LOG_GET = "OPERATION_LOG_GET"
    OPERATION_LOG_GET_ALL = "OPERATION_LOG_GET_ALL"




    STAFF_CREATE = "STAFF_CREATE"
    STAFF_EDIT = "STAFF_EDIT"
    STAFF_DELETE = "STAFF_DELETE"



PERMISSION_INFO = [
    {
        "category_name": "员工",
        "permissions": [
            {
                "key": PermissionKey.STAFF,
                "name": "员工管理",
                'notes': "查看员工管理页面， 员工的新增，修改等功能",
            }
        ]
    },

    {
        "category_name": "商品",
        "permissions": [
            {
                "key": PermissionKey.PRODUCT_GET,
                "name": "查看商品",
                'notes': "查看商品管理页面",
            },
            {
                "key": PermissionKey.PRODUCT_EDIT,
                "name": "编辑商品",
                'notes': "编辑商品",
            },
            {
                "key": PermissionKey.PRODUCT_DELETE,
                "name": "删除商品",
                'notes': "删除商品",
            },
        ]
    },

    {
        "category_name": "采购",
        "permissions": [
            {
                "key": PermissionKey.PURCHASE,
                "name": "采购单查看",
                'notes': "查看采购单页面",
            },
            {
                "key": PermissionKey.PURCHASE_GET_PURCHASE_PRICE,
                "name": "采购单查看采购价",
                'notes': "采购单查看采购价",
            },

        ]
    },

    {
        "category_name": "销售",
        "permissions": [
            {
                "key": PermissionKey.SALE,
                "name": "销售单查看",
                'notes': "查看销售单页面",
            },
        ]
    },

]


# 初始化 通用角色
def init_common_roles_script():
    roles_json = [
        {
            "name": "超级管理员",
            'notes': '超级管理员，拥有最大权限',
            'permissions': [
                PermissionKey.USER_GET,
                PermissionKey.USER_GET_ALL,
                PermissionKey.USER_CREATE,
                PermissionKey.USER_EDIT,
                PermissionKey.USER_DELETE,
                PermissionKey.ROLE_GET,
                PermissionKey.ROLE_CREATE,
                PermissionKey.ROLE_EDIT,
                PermissionKey.ROLE_DELETE,
                PermissionKey.PRODUCT_GET,
                PermissionKey.PRODUCT_EDIT,
                PermissionKey.PRODUCT_DELETE,
                PermissionKey.SALE,
                PermissionKey.PURCHASE_GET_PURCHASE_PRICE,
            ]
        },
        {
            "name": "管理员",
            'notes': '管理员工',
            'permissions': [
                PermissionKey.SALE, PermissionKey.PURCHASE_GET_PURCHASE_PRICE
            ]
        },
        {
            'name': '采购员',
            'notes': '负责商品的采购，包含采购单，采购退货等功能',
        },
        {
            'name': '销售员',
            'notes': '负责销售业务，包含销售单，销售退货单等功能',
        },
        {
            'name': '仓库管理员',
            'notes': '负责库存业务，包含仓库管理，库存盘点，库存调拨等功能',
        },
    ]
    for role_json in roles_json:
        role = Role.query.filter_by(name=role_json['name']).first()
        if not role:
            Role.create(**role_json)
        else:
            role.update(**role_json)
    db.session.commit()


if __name__ == '__main__':
    from app import create_app

    app = create_app("default")
    with app.app_context():
        g.user_id = None
        g.company_id = None
        init_common_roles_script()
